package com.gmail.dengtao.joe.group.channel.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.gmail.dengtao.joe.commons.util.RandomStringUtils;
import com.gmail.dengtao.joe.commons.util.StringUtils;
import com.gmail.dengtao.joe.group.channel.Channel;

public class ChannelFactory {

	public static final int CHANNEL_ID_LENGTH = 5;
	private static Map<String, Channel> channelMap = new ConcurrentHashMap<String, Channel>();
	
	public static void openChannel(Channel channel) {
		String id = StringUtils.EMPTY;
		do {
			id = RandomStringUtils.randomNumeric(CHANNEL_ID_LENGTH);
		} while (channelMap.containsKey(id));
		channel.setId(id);
		channelMap.put(id, channel);
	}
	
	public static void closeChannel(Channel channel) {
		if (StringUtils.isNotBlank(channel.getId())) {
			channelMap.remove(channel.getId());
		}
	}
}